{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ts_code</th>\n",
       "      <th>trade_date</th>\n",
       "      <th>turnover_rate</th>\n",
       "      <th>pe</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>000300.SH</td>\n",
       "      <td>20200107</td>\n",
       "      <td>0.51</td>\n",
       "      <td>13.86</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>000300.SH</td>\n",
       "      <td>20200106</td>\n",
       "      <td>0.64</td>\n",
       "      <td>13.78</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>000300.SH</td>\n",
       "      <td>20200103</td>\n",
       "      <td>0.52</td>\n",
       "      <td>13.82</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>000300.SH</td>\n",
       "      <td>20200102</td>\n",
       "      <td>0.66</td>\n",
       "      <td>13.83</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>000300.SH</td>\n",
       "      <td>20191231</td>\n",
       "      <td>0.45</td>\n",
       "      <td>13.66</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     ts_code trade_date  turnover_rate     pe\n",
       "0  000300.SH   20200107           0.51  13.86\n",
       "1  000300.SH   20200106           0.64  13.78\n",
       "2  000300.SH   20200103           0.52  13.82\n",
       "3  000300.SH   20200102           0.66  13.83\n",
       "4  000300.SH   20191231           0.45  13.66"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import tushare as ts\n",
    "import os\n",
    "import pandas as pd\n",
    "\n",
    "token = \"复制\"\n",
    "ts.set_token(token)\n",
    "pro = ts.pro_api()\n",
    "df = pro.index_dailybasic(ts_code = \"000300.SH\", fields='ts_code,trade_date,turnover_rate,pe')\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "千载难逢时机 可以选择历史分红多、净资产回报高、负债率小的\n"
     ]
    }
   ],
   "source": [
    "rate = df.pe.iloc[0]/df.pe.mean()  # df.pe.mean() tushare默认12年的pe\n",
    "# rate 低于70% 千载难逢时机 可以选择历史分红多、净资产回报高、负债率小的\n",
    "if rate < 0.5 :\n",
    "    # 一天发送一份邮件\n",
    "    print(\"千载难逢时机 可以选择历史分红多、净资产回报高、负债率小的\")\n",
    "elif rate < 0.7 :\n",
    "    # 一天发送一份邮件\n",
    "    print(\"可以开始定投 观察好公司逢低买入\")\n",
    "elif rate > 1 :  # 如果前三天都rate大于不发送\n",
    "    print(\"已经到到历史中位数 减半投入\")\n",
    "elif rate > 1.4 :  # 如果前三天都rate大于不发送\n",
    "    print(\"停止定投股票价格已经高了\")\n",
    "elif rate > 2 :\n",
    "    # 一天发送一份邮件\n",
    "    print(\"股票已经涨疯全部抛出把\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "时间段：2007-09-10 00:00:00 - 2020-01-07 00:00:00\n",
      "跟踪指数：000300.SH 沪深300  \n",
      "pe目前：13.86、12年中位数16.24、历史百分位0.85\n"
     ]
    }
   ],
   "source": [
    "strs = f\"\"\"时间段：{df.trade_date.iloc[-1]} - {df.trade_date.iloc[0]}\n",
    "跟踪指数：000300.SH 沪深300  \n",
    "pe目前：{df.pe.iloc[0]}、12年中位数{df.pe.mean():.4}、历史百分位{rate:.2}\"\"\"\n",
    "print(strs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
